package com.ys.malini.web.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ys.malini.dao.IProductDao;
import com.ys.malini.dao.IProductNewDao;
import com.ys.malini.model.TImages;
import com.ys.malini.web.form.ProductForm;

public class ProductNewAction extends DispatchAction {

	public IProductNewDao productNewDao;
	
	public IProductDao productDao;

	public IProductNewDao getProductNewDao() {
		return productNewDao;
	}

	public void setProductNewDao(IProductNewDao productNewDao) {
		this.productNewDao = productNewDao;
	}
	
	
	
	public IProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(IProductDao productDao) {
		this.productDao = productDao;
	}

	/**
	 * 查询前5位新品
	 * @param form
	 * @param isPage
	 * @return
	 */
	public ActionForward queryTop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	//	String classtype=request.getParameter("classtype");
		List<ProductForm> pdlist =productNewDao.qryTopProduct("");
		request.getSession().setAttribute("pdlist",pdlist);
		return mapping.findForward("listPage");
	}
	
	
	/**
	 * 查询单个新品明细
	 * 
	 * 套件页面用 product_new_detail.jsp,非套件页面用 product_new_detail_no.jsp
	 * 
	 */
	public ActionForward findNewProductById(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//查询主产品图片信息
		String pid = request.getParameter("pid");
		//是否套件
		String ptype = request.getParameter("ptype");
		//系列
		String series = request.getParameter("series");
		
		List<TImages> imageslist = productNewDao.queryDetailById(pid);
		request.setAttribute("imageslist", imageslist);
		
		Map map  = new HashMap();
		map.put("pid", pid);
		map.put("ptype", ptype);
		map.put("series", series);
		List<ProductForm> detailist = null;
		
		//查询新品 信息 
		if("0".equals(ptype)){
			//组合型
			detailist =productNewDao.queryDetailInfoById(map);
			if(detailist!=null&detailist.size()>0){
				ProductForm maindetail = (ProductForm)detailist.get(0);
				request.setAttribute("maindetail",maindetail);
			}
			
			request.setAttribute("detailist",detailist);//套件中的小件集合
			return mapping.findForward("detailPackage");			
		}else{
			//单个型
			ProductForm productdetail = productDao.queryDetailById(pid);
			request.setAttribute("productdetail", productdetail);
			
			ProductForm formbean = new ProductForm();
			formbean.setProductId(pid);
			formbean.setSeries(series);
			List<ProductForm> list =productDao.queryRelProdById(formbean);
			request.setAttribute("relist", list);
			return mapping.findForward("detail");
		}
		
		
	}
	
	
	/**
	 * 查询单个产品
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward findAllProductById(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		String pid = request.getParameter("pid");
		String series = request.getParameter("series");
		ProductForm productdetail = productDao.queryDetailById(pid);
		request.setAttribute("productdetail", productdetail);
		ProductForm formbean = new ProductForm();
		formbean.setProductId(pid);
		formbean.setSeries(series);
		List<ProductForm> list = productDao.queryRelProdById(formbean);
		request.setAttribute("relist", list);
		
		return mapping.findForward("detail");
		
	}
	
	
}
